import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:lib_uikit/lib_uikit.dart';

/// @Title   扇形图组件-半圆扇形图
/// @Author: bryce
/// @CreateDate:  2023年08月21日20:26:53
class ChartFanWidget extends StatefulWidget {
  final List<double> values;
  final List<Color> colors;
  final Widget Function(int index) buildCenterWidget;
  final double? width;
  final int defaultIndex;
  final bool enableTouch;
  final Function(int index)? touchCallback;

  const ChartFanWidget(
      {super.key,
      required this.values,
      required this.colors,
      required this.buildCenterWidget,
      this.defaultIndex = -1,
      this.enableTouch = true,
      this.touchCallback,
      this.width});

  @override
  State<StatefulWidget> createState() => _ChartFanWidgetState();
}

class _ChartFanWidgetState extends State<ChartFanWidget> {
  int touchedIndex = 0;
  double pieWidth = 20.0, touchedPieWidth = 28.0, sectionsSpace = 2;
  late double sumValue;

  @override
  void initState() {
    super.initState();
    touchedIndex = widget.defaultIndex;
    sumValue = widget.values.reduce((first, second) => first + second);
    debugPrint('----subValue = $sumValue');
  }

  @override
  Widget build(BuildContext context) {
    double width = widget.width ?? (MediaQuery.of(context).size.width - 100);
    return Stack(
      children: [
        ClipRect(
          child: Align(
            heightFactor: 0.5,
            alignment: Alignment.topCenter,
            child: ClipOval(
                child: SizedBox(
                    width: width,
                    height: width,
                    child: PieChart(
                      PieChartData(
                        pieTouchData: PieTouchData(
                          enabled: widget.enableTouch,
                          touchCallback: (FlTouchEvent event, pieTouchResponse) {
                            if (null != pieTouchResponse) {
                              int eventSectionIndex = pieTouchResponse.touchedSection!.touchedSectionIndex;
                              touchedIndex = eventSectionIndex;
                              setState(() {});
                              widget.touchCallback?.call(touchedIndex);
                            }
                          },
                        ),
                        borderData: FlBorderData(show: false),
                        sectionsSpace: sectionsSpace,
                        centerSpaceRadius: (width - touchedPieWidth * 2) / 2,
                        startDegreeOffset: 180,
                        sections: showingSections(),
                      ),
                    ))),
          ),
        ),
        Align(
          alignment: Alignment.topCenter,
          child: Padding(
              padding: GTEdgeInsetsOnly(top: width / 4 + 6),
              child: GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  touchedIndex = -1;
                  setState(() {});
                },
                child: widget.buildCenterWidget(touchedIndex),
              )),
        )
      ],
    );
  }

  List<PieChartSectionData> showingSections() {
    List<PieChartSectionData> sections = widget.values.asMap().entries.map((e) {
      final isTouched = e.key == touchedIndex;
      final radius = isTouched ? touchedPieWidth : pieWidth;

      return PieChartSectionData(color: widget.colors[e.key], value: e.value, showTitle: false, radius: radius);
    }).toList();

    sections.add(PieChartSectionData(color: Colors.transparent, value: sumValue, showTitle: false, radius: pieWidth));
    return sections;
  }
}
